//
//  AddTicketMemberVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2018/1/22.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "AddTicketMemberVC.h"
#import "TicketAddMemberInfoCell.h"
#import "TicketMemberInfoModel.h"

@interface AddTicketMemberVC ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong)NSMutableArray * dataSource;

@property (weak, nonatomic) IBOutlet UIButton *completeBtn;



@end

@implementation AddTicketMemberVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setup];
    [self loadData];
}


- (void)loadData {
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [NetAPIManager getTravelerListBlock:^(NSArray *TravelerList, NSError *error) {
       
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        if (TravelerList.count > 0) {
            
            self.dataSource = [NSMutableArray arrayWithArray:TravelerList];
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
    }];
    
    
}
- (NSMutableArray *)selectedMembers {
    
    if (!_selectedMembers) {
        
        _selectedMembers = [[NSMutableArray alloc] init];
    }
    
    return _selectedMembers;
}

- (IBAction)completeBtnClicked:(id)sender {
    
    if (_delegate && [_delegate respondsToSelector:@selector(completeAddWithMembers:Type:)]) {
        
        [_delegate  completeAddWithMembers:self.selectedMembers Type:self.type];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setup {
    
    
    [self.completeBtn setTitle:MYLocalizedString(@"MYQUESTION_SELECTPHOTO_DONE", nil) forState:UIControlStateNormal];
    [self.tableView registerNib:[UINib nibWithNibName:@"TicketAddMemberInfoCell" bundle:nil] forCellReuseIdentifier:@"TicketAddMemberInfoCell"];
    
    self.title = MYLocalizedString(@"TICKET_USER_TITLE", nil);
    
    [self setNavThemeGradient];
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self loadData];
    }];
    [self.tableView setThemeStyle:ThemeStyleBlack];
}


- (void)addMemberInfo {
    
    AddTicketMemberDetail * vc = [[AddTicketMemberDetail alloc] init];
    
    vc.mustLimitArray = [NSArray arrayWithArray:self.mustLimiteArray];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (TicketMemberInfoModel *)selectArrayIsContainWithTheModel:(TicketMemberInfoModel *)model{
    
    TicketMemberInfoModel * ticketModel = nil;
    for (TicketMemberInfoModel *member in self.selectedMembers) {
        
        if ([model.ID isEqualToString:member.ID]) {
            
            ticketModel = member;
            break;
        }
    }
    
    return ticketModel;
}

#pragma mark   --AddTicketMemberDetailDelegate

- (void)refreshData {
    
    [self loadData];
}
#pragma mark    --UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TicketAddMemberInfoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TicketAddMemberInfoCell"];
    
    cell.superVC = self;
    TicketMemberInfoModel * model = self.dataSource[indexPath.row];
    
    [cell configDataWith:model];
    
    if ([self selectArrayIsContainWithTheModel:model]) {
        
        cell.isSelect = YES;
    }
    
    return cell;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UITableViewHeaderFooterView * footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"footerView"];
    
    if (!footerView) {
        
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"footerView"];
        footerView.contentView.backgroundColor = [UIColor whiteColor];
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.layer.borderColor = kThemeColor.CGColor;
        btn.layer.borderWidth = 1.0f;
        [btn setTitleColor:kThemeColor forState:UIControlStateNormal];
        [btn setTitle:MYLocalizedString(@"TICKET_USER_ADD", nil) forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(addMemberInfo) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = Font(14);
        [footerView.contentView addSubview:btn];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(footerView.contentView.mas_centerX);
            make.centerY.equalTo(footerView.contentView.mas_centerY);
            make.width.mas_equalTo(kScreenWidth - 32);
            make.height.mas_equalTo(40);
        }];
        
    }
    
    return footerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TicketAddMemberInfoCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    
    TicketMemberInfoModel * model = self.dataSource[indexPath.row];
    
    TicketMemberInfoModel * member = [self selectArrayIsContainWithTheModel:model];
    if (member) {
        
        cell.isSelect = NO;
        [self.selectedMembers removeObject:member];
        
    }else{
        
        cell.isSelect = YES;
        [self.selectedMembers addObject:model];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 9;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
